//
//  ViewController.m
//  08.音乐播放器
//
//  Created by mac on 16/3/13.
//  Copyright © 2016年 itheima. All rights reserved.
//

#import "ViewController.h"
#import "Masonry.h"
#import "MJExtension.h"
#import "ZWMusicModel.h"
#import "ZWPlayMusicTool.h"
#import "ZWParseLyricTool.h"
#import "ZWLyricModel.h"
#import "ZWColorLabel.h"
#import "ZWLyricView.h"

@interface ViewController ()<ZWLyricViewDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *backgroundImageView;
@property (weak, nonatomic) IBOutlet UIButton *playButton;
@property (weak, nonatomic) IBOutlet UIButton *previousButton;
@property (weak, nonatomic) IBOutlet UIButton *nextButton;
@property (weak, nonatomic) IBOutlet UILabel *durationLabel;

@property (weak, nonatomic) IBOutlet UILabel *currentTimeLabel;
@property (weak, nonatomic) IBOutlet UISlider *slider;
@property (weak, nonatomic) IBOutlet UIView *centerView;
@property (weak, nonatomic) IBOutlet UILabel *singerLabel;

@property (weak, nonatomic) IBOutlet ZWColorLabel *lyricLabel;
@property (weak, nonatomic) IBOutlet UILabel *albumLabel;
@property (weak, nonatomic) IBOutlet UIImageView *singerIconImageView;
@property (weak, nonatomic) IBOutlet ZWLyricView *lyricView;

//歌曲模型数组
@property (strong,nonatomic) NSArray *musicArray;

//当前歌曲在数组中的索引
@property (assign,nonatomic) NSInteger currentMusicIndex;

//定时器
@property (strong,nonatomic) NSTimer *timer;

//保存歌词模型的数组
@property (strong,nonatomic) NSArray *lyricArray;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //给背景添加毛玻璃效果
    UIToolbar *toolbar = [[UIToolbar alloc]init];
    /*
     UIBarStyleDefault          = 0,
     UIBarStyleBlack            = 1,
     */
    toolbar.barStyle = UIBarStyleBlack;
    
    [self.backgroundImageView addSubview:toolbar];
    
    //利用masonry框架给毛玻璃添加约束
    [toolbar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];

    //配置UI界面
    [self configUI];
    
    self.lyricView.delegate = self;
    
}

#pragma mark - lyricView的 delegate
- (void)lyricView:(ZWLyricView *)lyricView scrollViewDidScroll:(CGFloat)progress{

    //centerView 的透明度随着 scrollView的滑动变化
    self.centerView.alpha = 1 - progress;
}

- (void)viewDidLayoutSubviews{

    [super viewDidLayoutSubviews];
    //singerIcon裁圆
    self.singerIconImageView.layer.cornerRadius = self.singerIconImageView.bounds.size.width * 0.5;
    self.singerIconImageView.layer.masksToBounds = YES;
}

#pragma mark - 配置UI界面
- (void)configUI{
    
    //工具类
    ZWPlayMusicTool *playMusicTool = [ZWPlayMusicTool sharedPlayMusic];
    
    //更新界面中不变的属性
    ZWMusicModel *model = self.musicArray[self.currentMusicIndex];
    
    //歌词解析
    self.lyricArray = [ZWParseLyricTool parseWithFileName:model.lrc];
    
    self.lyricView.lyricArray = self.lyricArray;
    
    //歌手
    self.singerLabel.text = model.singer;
    //头像/背景
    UIImage *image = [UIImage imageNamed:model.image];
    self.singerIconImageView.image = image;
    self.backgroundImageView.image = image;
    //专辑
    self.albumLabel.text = model.album;
    
    [self turnOffTimer];
    //调用播放按钮事件
    self.playButton.selected = NO;
    [self playBtnClick:nil];
    
    //总时间
    self.durationLabel.text = [self stringWithTimeInterval:playMusicTool.duration];

}

#pragma mark - 时间间隔转字符串
- (NSString *)stringWithTimeInterval:(NSTimeInterval)timeInterval{

//    NSLog(@"%f",timeInterval);
    int minute = timeInterval/60;
    int second = (int)timeInterval%60;
    
    return [NSString stringWithFormat:@"%02d:%02d",minute,second];
}
#pragma mark - 播放按钮点击事件
- (IBAction)playBtnClick:(id)sender {
    
    ZWPlayMusicTool *playMusicTool = [ZWPlayMusicTool sharedPlayMusic];
    ZWMusicModel *model = self.musicArray[self.currentMusicIndex];
    
    if (self.playButton.selected) {
        //正在播放,需要暂停
        [playMusicTool pause];
        [self turnOffTimer];
    }else{
        //此时状态是没有播放,需要播放
        [playMusicTool playMusicWithFileName:model.mp3 andDidComplete:^{
            [self nextBtnClick:nil];
        }];
        [self turnOnTimer];
    }
    //点击按钮后更改选中状态
    self.playButton.selected = !self.playButton.selected;
}
//上一首
- (IBAction)previousBtnClick:(id)sender {
    
    if (self.currentMusicIndex == 0) {
        self.currentMusicIndex = self.musicArray.count - 1;
    }else{
        self.currentMusicIndex --;
    }
    [self configUI];
}
//下一首
- (IBAction)nextBtnClick:(id)sender {
    if (self.currentMusicIndex == self.musicArray.count -1) {
        self.currentMusicIndex = 0;
    }else{
        self.currentMusicIndex ++;
    }
    [self configUI];
}
//滑块
- (IBAction)sliderChange:(id)sender {
    ZWPlayMusicTool *playMusicTool = [ZWPlayMusicTool sharedPlayMusic];
    playMusicTool.currentTime = self.slider.value * playMusicTool.duration;
    
}
//开启定时器
- (void)turnOnTimer{

    self.timer = [NSTimer scheduledTimerWithTimeInterval:0.1f target:self selector:@selector(updataWithTimer) userInfo:nil repeats:YES];
    
    //下面定时器  60次/秒
//    [CADisplayLink displayLinkWithTarget:self selector:@selector(updataWithTimer)];
    
}
//关闭定时器
- (void)turnOffTimer{

    [self.timer invalidate];
}
//定时器中需要更新的元素
- (void)updataWithTimer{

    ZWPlayMusicTool *tool = [ZWPlayMusicTool sharedPlayMusic];
    
    //头像转动
    self.singerIconImageView.transform = CGAffineTransformRotate(self.singerIconImageView.transform, M_PI * 0.01);
    //当前时间
    self.currentTimeLabel.text = [self stringWithTimeInterval:tool.currentTime];
    //slide的进度
    self.slider.value = tool.currentTime / tool.duration;
    //歌词
    [self updataWithLyric];
}
//更新歌词的方法
- (void)updataWithLyric{

    ZWPlayMusicTool *tool = [ZWPlayMusicTool sharedPlayMusic];
    
    for (int i = 0; i<self.lyricArray.count; i++) {
        ZWLyricModel *currentLyric = self.lyricArray[i];
        ZWLyricModel *nextLyric = nil;
        if (i == self.lyricArray.count - 1) {
            nextLyric = self.lyricArray[i];
        }else{
            nextLyric = self.lyricArray[i+1];
        }
        
        if (tool.currentTime > currentLyric.time && tool.currentTime < nextLyric.time) {
            self.lyricLabel.text = currentLyric.lyricString;
            self.lyricLabel.progress = (tool.currentTime - currentLyric.time) / (nextLyric.time - currentLyric.time);
            self.lyricView.currentLyricIndex = i;
            self.lyricView.progress = self.lyricLabel.progress;
        }
        
    }
}

#pragma mark - 懒加载
- (NSArray *)musicArray{

    if (_musicArray == nil) {
        _musicArray = [ZWMusicModel objectArrayWithFilename:@"mlist.plist"];
    }
    return _musicArray;
}


@end
